<?php

	include "kernel.php";
    include "config2.php";
	include "adw_functions.php";

    $link = ConnectToBase();

    //Obnulenie RANKA u vseh pilotov
    $r = mysql_query("update finalpilots set rank=0, rank_date='".time()."'");

    $r = mysql_query("
    	update
        	squadrons
        set
        	pot1=0,
        	pot2=0,
        	pot3=0,
        	pot4=0,
        	pot5=0,
        	pot6=0,
        	pot7=0,
        	pot8=0,
        	pot9=0,
        	pot10=0,
        	pot11=0,
        	pot12=0,
        	pot13=0,
        	pot14=0,
        	pot15=0,
        	pot16=0,
        	pot17=0,
        	pot18=0,
        	pot19=0,
        	pot20=0,
			rankpot=0,
			rgiven1=0,
			rgiven2=0,
		    rgiven3=0,
            rgiven4=0,
            rgiven5=0,
            rgiven6=0,
            rgiven7=0,
            rgiven8=0,
            rgiven9=0,
            rgiven10=0,
            squadpot=0,
            sgiven=0
            ");

    $r = mysql_query("select * from squadrons order by id");
    while ($res = mysql_fetch_assoc($r)) {

		$corank = GetCORank ($res[maxpilots]);
        $xorank = GetXORank ($res[maxpilots]);
        $rankpot = GetRankPot ($res[maxpilots]);

        mysql_query("update finalpilots set rank='$corank',rank_date='".time()."' where pilotid='".$res[comander]."'");
        mysql_query("update finalpilots set rank='$xorank',rank_date='".time()."' where pilotid='".$res[xcomader]."'");
        mysql_query("update squadrons set rankpot='$rankpot' where id='".$res[id]."'");

    }


    $r = mysql_query("
    	update
        	squadrons
        set
        	pot1=0,
        	pot2=0,
        	pot3=0,
        	pot4=0,
        	pot5=0,
        	pot6=0,
        	pot7=0,
        	pot8=0,
        	pot9=0,
        	pot10=0,
        	pot11=0,
        	pot12=0,
        	pot13=0,
        	pot14=0,
        	pot15=0,
        	pot16=0,
        	pot17=0,
        	pot18=0,
        	pot19=0,
        	pot20=0
            ");

    $r = mysql_query("
    	update
        	squadrons
        set
 			fuel = 0,
   			ammo = 0,
   			repair = 0,
   			addfuel = 0,
   			addammo = 0,
   			addrepair = 0,
            dfuel=0,
            dammo=0,
            drepair=0,
			eAir = 0,
			eBom = 0,
			eSAir = 0,
			eTank = 0,
			eCar = 0,
			eArt = 0,
			eAaa = 0,
			eWag = 0,
			eShip = 0,
			fAir = 0,
			fSAir = 0,
			fTank = 0,
			fCar = 0,
			fArt = 0,
			fAaa = 0,
			fWag = 0,
			fShip = 0,
			Wound = 0,
			deaths = 0,
			TKON = 0,
			BAIL = 0,
			CRASHED = 0,
			CAPTURED = 0,
			DIED = 0,
			MIA = 0,
			KICK = 0,
			DIEDISCO = 0,
            sorties=0,
            allsorties=0
            ");

    $r = mysql_query("
    	update
        	squadrons
        set
        	given1=0,
        	given2=0,
        	given3=0,
        	given4=0,
        	given5=0,
        	given6=0,
        	given7=0,
        	given8=0,
        	given9=0,
        	given10=0,
        	given11=0,
        	given12=0,
        	given13=0,
        	given14=0,
        	given15=0,
        	given16=0,
        	given17=0,
        	given18=0,
        	given19=0,
        	given20=0
            ");

    $r = mysql_query("
    	update
        	finalpilots
        set
        	award1=0,
        	award2=0,
        	award3=0,
        	award4=0,
        	award5=0,
        	award6=0,
        	award7=0,
        	award8=0,
        	award9=0,
        	award10=0,
        	award11=0,
        	award12=0,
        	award13=0,
        	award14=0,
        	award15=0,
        	award16=0,
        	award17=0,
        	award18=0,
        	award19=0,
        	award20=0,
        	award21=0,
        	award22=0,
        	award23=0,
        	award24=0,
        	award25=0,
        	award26=0,
        	award27=0,
        	award28=0,
        	award29=0,
        	award30=0,
        	award31=0,

        	award1_date=0,
        	award2_date=0,
        	award3_date=0,
        	award4_date=0,
        	award5_date=0,
        	award6_date=0,
        	award7_date=0,
        	award8_date=0,
        	award9_date=0,
        	award10_date=0,
        	award11_date=0,
        	award12_date=0,
        	award13_date=0,
        	award14_date=0,
        	award15_date=0,
        	award16_date=0,
        	award17_date=0,
        	award18_date=0,
        	award19_date=0,
        	award20_date=0,
        	award21_date=0,
        	award22_date=0,
        	award23_date=0,
        	award24_date=0,
        	award25_date=0,
        	award26_date=0,
        	award27_date=0,
        	award28_date=0,
        	award29_date=0,
        	award30_date=0,
        	award31_date=0,

            fuel=0,
            addfuel=0,
            dfuel=0,

            ammo=0,
            addammo=0,
            dammo=0,

            repair=0,
            addrepair=0,
            drepair=0
            ");

	$TPmain->Necho = 0;
	$bd = mktime();
	$d = $bd;

	$allpilots_r = mysql_query("select * from finalpilots order by id");

    $i=0;
    while ($pilot = mysql_fetch_assoc($allpilots_r))
    {
    	$i++;

echo "$squadronid";

        $squadronid =$pilot[squadronid];
    	$pilot = $pilot[pilotid];

     	ob_start();

        $link = ConnectToBase ();

        echo "PARSING PILOT #$pilot\r\n";

        echo "Update_Pilot_Common_Stat...\r\n";

		UpdatePilotCommonStat ($pilot);

        $updates = mysql_query("update sorties set UpdateSorties='0' where pilotid='$pilot'");

        $sorties = mysql_query("select id from sorties where pilotid='$pilot' order by id DESC");

         while ($sort = mysql_fetch_assoc($sorties)) {

    			$SUM = 0;

         		$id = $sort[id];

                UpdateSquadronCommonStat($id,$squadronid);

	            $pilot_r = mysql_query("select UNIX_TIMESTAMP(starttime) as s,
	                                           UNIX_TIMESTAMP(endtime) as e,
	                                           pilotid,

	                                           (eSAir+eTank+eCar+eArt+eAaa+eWag+eShip) as ground,

	                                           eAir-eBom as air,
	                                           eBom as bom,

	                                            addfuel,
	                                            addammo,
	                                            addrepair,
	                                            dfuel,
	                                            dammo,
	                                            drepair,

	                                           eSAir as sair,
	                                           eTank as tank,
	                                           eCar as car,
	                                           eArt as art,
	                                           eAaa as aaa,
	                                           eWag as wag,
	                                           eShip as ship,

	                                           SortieEND as end,
	                                           Wound as wound,
	                                           UpdateSorties as upd

	                       from sorties where id='$id'");

	            $sortie = mysql_fetch_assoc($pilot_r);
	            $s = $sortie[s];
	            $e = $sortie[e];
	            $end = $sortie[end];
	            $upd = $sortie[upd];
	            $tank = $sortie[tank];
	            $sair = $sortie[sair];
	            $art = $sortie[art];
	            $aaa = $sortie[aaa];
	            $car = $sortie[car];
	            $wag = $sortie[wag];
	            $ship = $sortie[ship];
	            $air = $sortie[air]-$sortie[bom];
	            $bom = $sortie[bom];
	            $ground = $sortie[ground];
	            $w = $sortie[wound];

	            $pilot = $sortie[pilotid];

	            if (($end != 'MIA' and $end != 'KICK' and $end != 'DIEDISCO') and ($e - $s > 15*60 or $end != 'TKON' or $air>0 or $ground>0  or $sortie[addammo]>0 or $sortie[addfuel]>0 or $sortie[addrepair]>0 or $sortie[dfuel]>0 or $sortie[dammo]>0 or $sortie[drepair]>0))
	                $fightflight = 1;
	            else
	                $fightflight = 0;


	            $sifd = $sortie[end];
	            if ($sifd == 'DIED')
	                $SUM -= 40;
	            elseif ($sifd=='CAPTURED')
	                $SUM -= 40;
	            elseif ($sifd=='DIEDISCO')
	                $SUM -= 40;
	            elseif ($sifd=='MIA')
	                $SUM -= 20;
	            elseif ($sifd=='BAIL')
	                $SUM -= 20;
	            elseif ($sifd=='CRASHED')
	                $SUM -= 10;

	            if ($sifd=='TKON' and $w)
	                $SUM += 7;

	            if ($sifd=='TKON' and $fightflight)
	                $SUM += 5;

	            $SUM += ($air*30 + $bom*60 + $tank*7 + $art*3 + $aaa*5 + $sair*10 + $wag*1 + $car*1 + $ship*100);

	            $link = ConnectToBase ();
	            if (!$upd) {
	            	mysql_query("update sorties set UpdateSorties='1' where id='$id'");
	            }

	            UpdateSquadSums($SUM,$pilot);

	            if ($fightflight) {
	                mysql_query("update squadrons set sorties=sorties+1 where id='$squadronid'");
	                mysql_query("update squadrons set allsorties=allsorties+1 where id='$squadronid'");
	            }
	            else
	                mysql_query("update squadrons set allsorties=allsorties+1 where id='$squadronid'");


        }



		$d = mktime() - $d;
    	echo "$i. Done in ".$d."\r\n";
        $d = mktime();

        echo "\r\n";
        ob_end_flush();

    }

echo "lets recount all missions\r\n";

$r = mysql_query("select id from mission order by id");

while ($mid = mysql_fetch_assoc($r)) {

	$mid = $mid[id];

	$r2 = mysql_query("select
                        UNIX_TIMESTAMP(starttime) as s,
        							   UNIX_TIMESTAMP(endtime) as e,
                                       id,
                                       pilotid,

                                       (eSAir+eTank+eCar+eArt+eAaa+eWag+eShip) as ground,

						               eAir-eBom as air,
                                       eBom as bom,

                                       eSAir as sair,
                                       eTank as tank,
                                       eCar as car,
                                       eArt as art,
                                       eAaa as aaa,
                                       eWag as wag,

									   SortieEND as end,
                                       Wound as wound,
						               UpdateSorties as upd,
				                       pilotid,
                                       addfuel,
                                       addammo,
                                       addrepair,
                                       dfuel,
                                       dammo,
                                       drepair
                        from
                        				sorties where missionid='".$mid."' order by id");

	    while($sortie = mysql_fetch_assoc($r2)) {

	            $SUM = 0;

	            $pilot = $sortie[pilotid];
	            $fpilot_r = mysql_query("select squadronid from pilots where id='".$pilot."'");
	            $squadronid = mysql_fetch_assoc($fpilot_r);
	            $squadronid = $squadronid[squadronid];

	            $s = $sortie[s];
	            $e = $sortie[e];
	            $end = $sortie[end];

	            $upd = $sortie[upd];
	            $tank = $sortie[tank];
	            $sair = $sortie[sair];
	            $art = $sortie[art];
	            $aaa = $sortie[aaa];
	            $car = $sortie[car];
	            $wag = $sortie[wag];
	            $ship = $sortie[ship];
	            $air = $sortie[air]-$sortie[bom];
	            $bom = $sortie[bom];
	            $ground = $sortie[ground];
	            $w = $sortie[wound];

	            $SUM += ($sortie[dfuel]*0.5 + $sortie[dammo]*0.5 + $sortie[drepair]*0.5);
	            $SUM += ($sortie[addfuel]*10 + $sortie[addammo]*10 + $sortie[addrepair]*10);

	            UpdateSquadSums($SUM,$pilot);

	            mysql_query("update squadrons set addammo=addammo+'".$sortie[addammo]."', addfuel=addfuel+'".$sortie[addfuel]."', addrepair=addrepair+'".$sortie[addrepair]."', dfuel=dfuel+".$sortie[dfuel].",dammo=dammo+".$sortie[dammo].",drepair=drepair+".$sortie[drepair]." where id='".$squadronid."'");
echo "update squadrons set addammo=addammo+'".$sortie[addammo]."', addfuel=addfuel+'".$sortie[addfuel]."', addrepair=addrepair+'".$sortie[addrepair]."', dfuel=dfuel+".$sortie[dfuel].",dammo=dammo+".$sortie[dammo].",drepair=drepair+".$sortie[drepair]." where id='".$squadronid."'\r\n";
	            mysql_query("update finalpilots set addammo=addammo+'".$sortie[addammo]."', addfuel=addfuel+'".$sortie[addfuel]."', addrepair=addrepair+'".$sortie[addrepair]."', dfuel=dfuel+".$sortie[dfuel].",dammo=dammo+".$sortie[dammo].",drepair=drepair+".$sortie[drepair]." where pilotid='".$pilot."'");
echo "update finalpilots set addammo=addammo+'".$sortie[addammo]."', addfuel=addfuel+'".$sortie[addfuel]."', addrepair=addrepair+'".$sortie[addrepair]."', dfuel=dfuel+".$sortie[dfuel].",dammo=dammo+".$sortie[dammo].",drepair=drepair+".$sortie[drepair]." where pilotid='".$pilot."'\r\n";

	            if ($SUM>0 and !($e - $s > 15*60 or ($end != 'TKON' and $end != 'MIA' and $end != 'KICK') or ($air>0 or $ground>0))) {
                	echo "Squad:$squadronid, sortie:".$sortie[id]."\r\n";
	            }

	    }

}

        $bd = mktime() - $bd;
    	echo "Script done. \r\n Pilots parsed: $i \r\n In $bd seconds\r\n";



?>